#include <Precomp.h>
#include <GameWorld.h>
#include <Player.h>
#include <GameSoundManager.h>
#include <SoundManager.h>

CGameSoundManager::CGameSoundManager(CGameWorld* pGameWorld, CSoundManager* pSoundManager) : m_pGameWorld(pGameWorld), m_pSoundManager(pSoundManager)
{
    m_pEventSubscription = m_pGameWorld->SubscribeEvents( CGameWorld::EventSubscriber(&CGameSoundManager::OnGameWorldEvent, this));
}

CGameSoundManager::~CGameSoundManager()
{
    m_pGameWorld->UnsubscribeEvents(m_pEventSubscription);
}


void CGameSoundManager::Update()
{
    CGamePlayer* pPlayer = m_pGameWorld->GetPlayerForLocalPlayer();

    if(pPlayer != NULL)
    {
        m_pSoundManager->SetListenerPosition(Ogre::Vector3(pPlayer->GetRenderedXLocation(), pPlayer->GetRenderedYLocation(), 0.0f));
    }
}

void CGameSoundManager::OnGameWorldEvent(CGameWorldEvent* pEvent)
{
    if(pEvent->GetEventID() == eGameWorldEventID_TorpFired)
    {
        CTorp* pTorp = ((CTorpEvent*)pEvent)->GetTorp();

        m_pSoundManager->PlaySound3D(eSoundType_TorpFired, 
                                     Ogre::Vector3(pTorp->GetOwner()->GetRenderedXLocation(), pTorp->GetOwner()->GetRenderedYLocation(), 0.0f));
    }
    if(pEvent->GetEventID() == eGameWorldEventID_TorpHit)
    {
        CTorp* pTorp = ((CTorpEvent*)pEvent)->GetTorp();

        m_pSoundManager->PlaySound3D(eSoundType_TorpHit, 
                                     Ogre::Vector3(pTorp->GetXLocation() * 1.0f, pTorp->GetYLocation() * 1.0f, 0.0f));
    }
    else if(pEvent->GetEventID() == eGameWorldEventID_PhaserFired)
    {
        CPhaser* pPhaser = ((CPhaserEvent*)pEvent)->GetPhaser();

        m_pSoundManager->PlaySound3D(eSoundType_PhaserFired, 
                                     Ogre::Vector3(pPhaser->GetOwner()->GetRenderedXLocation(), pPhaser->GetOwner()->GetRenderedYLocation(), 0.0f));

    }
    else if(pEvent->GetEventID() == eGameWorldEventID_PlayerCloaked)
    {
        CGamePlayer* pPlayer = ((CPlayerEvent*)pEvent)->GetPlayer();

        m_pSoundManager->PlaySound3D(eSoundType_PlayerCloaked, 
                                     Ogre::Vector3(pPlayer->GetRenderedXLocation(), pPlayer->GetRenderedYLocation(), 0.0f));

    }
    else if(pEvent->GetEventID() == eGameWorldEventID_PlayerDecloaked)
    {
        CGamePlayer* pPlayer = ((CPlayerEvent*)pEvent)->GetPlayer();

        m_pSoundManager->PlaySound3D(eSoundType_PlayerDecloaked, 
                                     Ogre::Vector3(pPlayer->GetRenderedXLocation(), pPlayer->GetRenderedYLocation(), 0.0f));
    }
    else if(pEvent->GetEventID() == eGameWorldEventID_TractorBegin)
    {
//        m_pSoundManager->PlaySound(eSoundType_Tractor, ePlaySoundFlags_Loop);
    }
    else if(pEvent->GetEventID() == eGameWorldEventID_TractorEnd)
    {
        m_pSoundManager->StopSound(eSoundType_Tractor);
    }

}
